package cn.ycl.auth.biz.usercenter.controller;


import cn.ycl.auth.biz.usercenter.dao.UserDao;
import cn.ycl.auth.biz.usercenter.entity.User;
import cn.ycl.auth.biz.usercenter.service.UserService;
import cn.ycl.syslog.OperationEnum;
import cn.ycl.syslog.SystemLog;
import cn.ycl.constans.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-10-12 14:22:25
 */
@RestController
@RequestMapping("/user")
public class UserController implements Serializable {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    /**
     * 以下是为了测试sentinel的流量控制、熔断降级等
     *
     * @return String
     */
    @GetMapping(value = "/hello")
//    @SentinelResource("hello")
    public String hello() {

        return "Hello Sentinel";
    }

    @GetMapping(value = "/hello22")
//    @SentinelResource("hello222")
    public String hello2() {
        return "Hello Sentinel222";
    }
    //---------------------------------------------

    /**
     * 通过userId查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{userId}")
    public User selectOne(@PathVariable("userId") Long userId) {
        return this.userService.queryByUserId(userId);
    }


    /**
     * 修改数据
     *
     * @return 请求结果
     */
    @SystemLog(value = "编辑用户", type = OperationEnum.USER_MANAGE)
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        Integer update = userService.update(user);
        return Result.ok(update > 0);
    }

    /**
     * 删除数据
     *
     * @param userId 前端需要传的数据
     * @return 请求结果
     */
    @SystemLog(value = "删除用户", type = OperationEnum.USER_MANAGE)
    @PostMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable("userId") Long userId) {
        int result = userService.deleteByUserId(userId);
        return Result.ok(result > 0);
    }

    /**
     * 新增数据
     *
     * @param user 前端需要传的数据
     * @return 请求结果
     */
    @PostMapping("/insertUser")
    @SystemLog(value = "添加用户", type = OperationEnum.USER_MANAGE)
    public Result insertProduct(@RequestBody User user) {
        Integer insert = userService.insert(user);
        return Result.ok(insert > 0);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param user 实例对象
     * @return 对象列表
     */
    @SystemLog(value = "进入用户管理", type = OperationEnum.USER_MANAGE)
    @GetMapping("/queryByUser")
    public PageInfo<User> queryByUser(User user) {
        //在下面两行代码尽量凑在一起，别分开
        PageHelper.startPage(user.getPageNum(), user.getPageSize());
        List<User> users = userService.queryByUser(user);
        return new PageInfo<>(users);
    }

    //---------------------------------------------

    /**
     * 通过用户名查找用户
     *
     * @param userName 用户名
     * @return 结果
     */
    @GetMapping("/queryByUserName/{userName}")
    public User queryByUserName(@PathVariable("userName") String userName) {
        return userService.queryByUserName(userName);
    }


}